'''
Created on Jun 2, 2010

@author: oabalbin
'''

import numpy as np

####
import signatures.db.query as dq
import signatures.parsers.read_gene_lists as pg
import signatures.db.query as dq
import signatures.parsers.read_protein_db as pp
from collections import deque, defaultdict
### 

def write_p2p_edges_byBait_dhea(interactomeDict,nodelistDict,outputfile):
    """
    writes a file for the edges in the interactome
    #nodeA, nodeB, score
    """
    header = ["#nodeA","nodeB","score"]
    outputfile.write(",".join(header).replace(",","\t")+'\n')
    
    for gene,interactions in interactomeDict.iteritems():
        for prot in interactions:
            score = np.random.uniform(low=0.0, high=1.0)
            gen_ind, prot_ind = nodelistDict[gene], nodelistDict[prot] 
            #print gene+'\t'+prot+'\t'+str(score)
            #outputfile.write(gene+'\t'+prot+'\t'+str(score)+'\n')       
            outputfile.write(str(gen_ind)+'\t'+str(prot_ind)+'\t'+str(score)+'\n')


def write_p2p_nodes_dhea(nodelist,nodelistDict,outputfile):
    """
    writes a file for the node in the interactome
    #label score
    """
    for  node in nodelist:
        score = np.random.uniform(low=0.0, high=1.0)
        node_ind = nodelistDict[node]
        #outputfile.write(node+'\t'+str(score)+'\n')
        outputfile.write(str(node_ind)+'\t'+str(score)+'\n')


def get_nodes_from_interactomeDict(interactomeDict):
    """
    return the nodes in the interactome dict
    """
    nodelist=[]
    nodelist_keys=interactomeDict.keys()
    nodelist.append(nodelist_keys)
    
    for node, interactions in interactomeDict.iteritems():
        nodelist.append(interactions) 
    
    nodelist = sum(nodelist,[])
    return list(set(nodelist))

    
def map_nodes_names2number(nodelist):
    """
    returns a dictionary with the nodes mapped to an integer, because that it is the input for the dhea 
    """
    nodelistDict=defaultdict()
    for indicator, node in enumerate(nodelist):
        nodelistDict[node] = indicator
    
    return nodelistDict
        


###########################
###########################

prt = pp.protparser("localhost", "oabalbin", "oscar", "signatures")
mgp = pg.geneparser()
cell_oultliersList = mgp.list_of_names_in_line_dictionary(open('/home/oabalbin/projects/networks/Kinases_lists/all_outliers_onlygenes.txt'))
#interactome = prt.get_p2p_interactome(geneList)
i=0
for cell, geneList in cell_oultliersList.iteritems():
    print cell, geneList
    interactome = prt.get_p2p_interactome_byBait(geneList)
    #outputfilename = '/home/oabalbin/projects/networks/Kinases_lists/output/first_outliers_genes_interactome_'+cell+'_'+str(i)+'.db'
    outputfilename = '/data/projects/pcst/test/alledges_'+cell+'_'+str(i)+'.txt'
    outputfilename2 = '/data/projects/pcst/test/allnodes_'+cell+'_'+str(i)+'.txt'
    
    nodelist = get_nodes_from_interactomeDict(interactome)
    nodelistDict = map_nodes_names2number(nodelist)
    write_p2p_edges_byBait_dhea(interactome,nodelistDict,open(outputfilename,'w'))
    write_p2p_nodes_dhea(nodelist,nodelistDict, open(outputfilename2,'w'))
    i+=1
